package com.lwj.liwanjia.view.activity.work.shang_jia.spfabu;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.fabu.FabuTypeAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.fabu.FabuTypeBean;
import com.lwj.liwanjia.impl.FabuTypeView;
import com.lwj.liwanjia.presenter.FabuTypePresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/13.
 */

public class TypeActivity extends BaseActivity implements FabuTypeView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_select)
    SuperRecyclerView rlSelect;

    private List<FabuTypeBean.DataBean> list = new ArrayList<>();
    private FabuTypeAdapter adapter;
    private FabuTypePresenter presenter;
    private int id = -1;
    private String type;
    private List<Integer> idList = new ArrayList<>();//用来存储每层对应的id
    private List<String> typeList = new ArrayList<>();//存储每层的类型

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("类目");
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rlSelect.setLayoutManager(manager);
        rlSelect.setRefreshEnabled(false);
        rlSelect.setLoadMoreEnabled(false);
        adapter = new FabuTypeAdapter(this, list);
        rlSelect.setAdapter(adapter);
        presenter = new FabuTypePresenter(this);
        listener();
        //默认第一层id为-1(方便返回重新掉接口)
        idList.add(id);
        typeList.add("-1");
    }

    private void listener() {
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                id = list.get(position).getId();
                type = list.get(position).getScope();
                idList.add(id);
                typeList.add(type);
                initData();
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getTypeData(SpfUtils.getSpfUtils(this).getToken(), idList.get(idList.size() - 1));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list;
    }

    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        //如果最上面一层，销毁Activitiy,回到发布界面
        if (idList.size() > 1) {
            typeList.remove(typeList.size() - 1);
            idList.remove(idList.size() - 1);
            initData();
        } else {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        if (idList.size() > 1) {
            typeList.remove(typeList.size() - 1);
            idList.remove(idList.size() - 1);
            initData();
        } else {
            finish();
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getTypeData(FabuTypeBean fabuTypeBean) {
        if (fabuTypeBean.getState() == 0) {
            if (fabuTypeBean.getMessage().equals("没有下一级分类")) {
                Intent intent = new Intent();
                intent.putExtra("type", type);
                idList.remove(0);
                typeList.remove(0);
                intent.putIntegerArrayListExtra("idList", (ArrayList<Integer>) idList);
                intent.putStringArrayListExtra("typeList", (ArrayList<String>) typeList);
                setResult(RESULT_OK, intent);
                finish();
            }
        } else if (fabuTypeBean.getState() == 1) {
            list.clear();
            list.addAll(fabuTypeBean.getData());
            adapter.notifyDataSetChanged();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter = null;
        }
    }
}
